import re
import sys
import os

def remove_comments(code):
    """去掉 C 代码中的单行和多行注释"""
    code = re.sub(r'/\*[\s\S]*?\*/', '', code)  # 多行注释
    code = re.sub(r'//.*', '', code)            # 单行注释
    return code

def extract_chinese_from_file(filename, seen):
    with open(filename, 'r', encoding='utf-8') as f:
        code = f.read()
    code_no_comments = remove_comments(code)

    # 提取所有中文字符
    chinese_chars = re.findall(r'[\u4e00-\u9fff]', code_no_comments)

    # 去重并保留顺序（跨文件）
    for c in chinese_chars:
        if c not in seen:
            seen.add(c)

def process_path(path, seen):
    if os.path.isfile(path) and path.lower().endswith(('.c', '.h')):
        extract_chinese_from_file(path, seen)
    elif os.path.isdir(path):
        for root, _, files in os.walk(path):
            for file in files:
                if file.lower().endswith(('.c', '.h')):
                    extract_chinese_from_file(os.path.join(root, file), seen)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("用法: python find_chinese.py <文件或文件夹路径>")
    else:
        seen_chars = set()
        for p in sys.argv[1:]:
            process_path(p, seen_chars)

        # 输出去重后的中文
        print(''.join(seen_chars))
